package com.xu.t1.mapper;

import com.xu.t1.model.LearningRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 负责操作用户学习记录数据的持久化接口类
 *
 * @author xu
 *
 */

@Mapper
public interface LearningRecordMapper {

    // 查询用户的学习记录
    @Select("SELECT * FROM learning_records WHERE user_id = #{userId}")
    List<LearningRecord> findRecordsByUserId(Long userId);

    // 分页查询用户学习记录
    @Select("SELECT * FROM learning_records WHERE user_id=#{userId} " +
            "ORDER BY study_date LIMIT #{size}  OFFSET #{offset}")
    List<LearningRecord> findRecordsByUserIdPaged(@Param("userId") Long userId,
                                                  @Param("offset") int offset,
                                                  @Param("size") int size);

    @Select("SELECT COUNT(*) FROM learning_records WHERE user_id = #{userId}")
    int countByUserId(@Param("userId") Long userId);

    // 上传用户的学习记录
    @Insert("INSERT INTO learning_records (user_id, study_date, study_time, progress, content) " +
            "VALUES (#{userId}, #{studyDate}, #{studyTime}, #{progress}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertRecord(LearningRecord record);

    // 删除用户的学习记录通过主键
    @Delete("DELETE FROM learning_records WHERE  id =#{id}")
    int deleteRecordById(Long id);


    //批量插入
    int insertRecords(@Param("list") List<LearningRecord> records);

}

